home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / Rectangle.h < prev    next >
C/C++ Source or Header  |  1992-04-27  |  4KB  |  170 lines

  1. #ifndef Rectangle_First
  2. #ifdef __GNUG__
  3. //pragma once
  4. #pragma interface
  5. #endif
  6. #define Rectangle_First
  7.  
  8. #include "Metric.h"
  9.  
  10. class Rectangle {
  11. public:
  12.     Point origin;
  13.     Point extent;
  14.       
  15. public:
  16.     
  17.     Rectangle();
  18.  
  19.     Rectangle(int x, int y, int w, int h);
  20.  
  21.     Rectangle(Point o, Point e);
  22.     
  23.     Rectangle(Point e);
  24.  
  25.     Rectangle(int w, int h);
  26.     
  27.     Rectangle(const Rectangle&);
  28.     
  29.     int Width() const
  30.     { return extent.x; }
  31.  
  32.     int Height() const
  33.     { return extent.y; }
  34.  
  35.     int Left() const
  36.     { return origin.x; }
  37.  
  38.     int Top() const
  39.     { return origin.y; }
  40.     
  41.     Point Center() const
  42.     { return Point(origin.x+extent.x/2, origin.y+extent.y/2); }
  43.  
  44.     Point NW() const
  45.     { return origin; }
  46.     
  47.     Point N() const
  48.     { return Point(origin.x+extent.x/2, origin.y); }
  49.  
  50.     Point NE() const
  51.     { return Point(origin.x+extent.x-1, origin.y); }
  52.  
  53.     Point E() const
  54.     { return Point(origin.x+extent.x-1, origin.y+extent.y/2); }
  55.     
  56.     Point SE() const
  57.     { return origin + extent - 1; }
  58.     
  59.     Point S() const
  60.     { return Point(origin.x+extent.x/2, origin.y+extent.y-1); }
  61.     
  62.     Point SW() const
  63.     { return Point(origin.x, origin.y+extent.y-1); }
  64.  
  65.     Point W() const
  66.     { return Point(origin.x, origin.y+extent.y/2); }
  67.         
  68.     friend Rectangle NormRect(Point p1, Point p2);
  69.     
  70.     friend Rectangle BoundingBox(int, Point *pts, Point *npts= 0);
  71.  
  72.     int Area () const
  73.     { return extent.x * extent.y; }
  74.  
  75.     Rectangle &Moveby (Point p)
  76.     { origin+= p; return *this; }
  77.     
  78.     friend Rectangle Moveby (Rectangle r, Point p)
  79.     { r.origin+= p; return r; }
  80.     
  81.     Rectangle operator+= (Point p)
  82.     { origin+= p; return *this; }
  83.     
  84.     Rectangle operator+ (Point p)
  85.     { return Rectangle(origin+p, extent); }
  86.     
  87.     Rectangle operator- (Point p)
  88.     { return Rectangle(origin-p, extent); }
  89.     
  90.     Rectangle operator-= (Point p)
  91.     { origin-= p; return *this; }
  92.  
  93.     void Moveto (Point p)
  94.     { origin= p; }
  95.  
  96.     void Scaleby (Point p)
  97.     { extent*= p; }
  98.  
  99.     friend bool operator== (const Rectangle &r1, const Rectangle &r2)
  100.     { return (bool) (r1.origin == r2.origin && r1.extent == r2.extent); }
  101.  
  102.     friend bool operator!= (const Rectangle &r1, const Rectangle &r2)
  103.     { return (bool) (r1.origin != r2.origin || r1.extent != r2.extent); }
  104.     
  105.     friend Rectangle Union(const Rectangle &r1, const Rectangle &r2);
  106.  
  107.     friend Rectangle Inter(Rectangle r1, const Rectangle &r2);
  108.  
  109.     friend int Difference(Rectangle *rp, const Rectangle &r1, const Rectangle &r2);
  110.     
  111.     Rectangle Intersect(const Rectangle &r);
  112.  
  113.     bool Clip(const Rectangle &r);
  114.     
  115.     bool ContainsRect(const Rectangle &r) const;
  116.     bool OvalContainsRect(const Rectangle &r) const;
  117.     
  118.     Rectangle Inset(Point p);
  119.  
  120.     Rectangle Expand(Point p);
  121.     
  122.     Rectangle Merge(const Rectangle &r);
  123.  
  124.     bool ContainsPoint(Point p) const
  125.     { return (bool) (p >= origin && p < origin + extent); }
  126.  
  127.     bool Intersects(const Rectangle &r) const;
  128.           
  129.     bool IsEmpty() const
  130.     { return (bool) (extent.x <= 0 || extent.y <= 0); }
  131.         
  132.     bool IsNotEmpty() const
  133.     { return (bool) (extent.x > 0 && extent.y > 0); }
  134.  
  135.     Point Constrain(Point p);
  136.     
  137.     friend Point ConstrainMoveRect(const Rectangle &r1, const Rectangle &r2, Point delta);
  138.  
  139.     Point AmountToTranslateWithin(const Rectangle &r);
  140.  
  141.     int PointToAngle(Point) const;
  142.     
  143.     Point AngleToPoint(int) const;
  144.     Point OvalAngleToPoint(int) const;
  145.         
  146.     Rectangle WedgeBBox(int s, int e);
  147.     
  148.     //         7|0|1
  149.     //        -------
  150.     //         6| |2
  151.     //        -------
  152.     //         5|4|3
  153.  
  154.     int PointToCorner(Point p) const;
  155.  
  156.     Point CornerToPoint(int n) const;
  157.     
  158.     friend OStream& operator<< (OStream &s, const Rectangle &r);
  159.            
  160.     friend IStream& operator>> (IStream &s, Rectangle &r);
  161.     
  162.     char *AsString() const;
  163. };
  164.  
  165. SimpleMetaDef(Rectangle);
  166.  
  167. extern const Rectangle gRect0;
  168.  
  169. #endif
  170.